<template>
  <div class="app-bg auto">
    <tools-page :need-back='true' title="安检计划"  @back="back()"></tools-page>
      <div class="row app-input">
        <label class="col-xs-12 control-label text-left">{{context}}</label>
      </div>
      <div class="row app-btn" style="margin-bottom: -5px;">
        <!-- <button class="btn btn-primary col-xs-6" @click= "planinfomation()"><span class="glyphicon glyphicon-zoom-in" style="margin-right: 20px;"></span>查询</button> -->
        <button class="btn btn-primary col-xs-6" @click= "upload()"><span class="glyphicon glyphicon-circle-arrow-up" style="margin-right: 20px;"></span>上传</button>
      </div>
      </div>

    <div v-for='row in result' class="app-list auto" >
      <div class="row" style="color: #00B9F1;">
        <span class="col-xs-3">户号：</span><span class="col-xs-3">{{row.f_userid}}</span>
        <span class="col-xs-3">姓名：</span><span class="col-xs-3">{{row.f_username}}</span>
      </div>
      <div class="row">
        <span class="col-xs-3">表号：</span><span class="col-xs-3">{{row.f_meternumber}}</span>
        <span class="col-xs-3">用户类型：</span><span class="col-xs-3">{{row.f_usertype}}</span>
      </div>
      <div class="row">
        <span class="col-xs-3">地址：</span><span class="col-xs-8">{{row.f_address}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import co from 'co'
import Vue from 'vue'
export default {
  title:'抄表用户信息',
  data () {
    return {
      context: '',
      // 未上传成功的数据
      result: [],
      // 保存数据
      resultdata: []
    }
  },
  methods: {
    //头部事件回掉
    back () {
      this.$back()
    },
    //根据条件拿到用户
    planinfomation () {
        let infomathis = info(this)
        co(infomathis)
    },
    // 上传所有未成功上传的数据
    upload() {
      this.resultdata = this.result
      for (var i = 0; i < this.resultdata.length; i++) {
        delete this.resultdata[i].id
      }
      this.$post(Vue.url + 'logic/savecheck', {data: {result: this.resultdata,flag: 0}},
    { resolveMsg: null, rejectMsg: null })
      .then((res)=> {
        this.$showMessage ('上传数据成功')
        //删除上传成功的
        HostApp.__callback__ = this.callback
        HostApp.__this__ = this
         HostApp.deletedata({id: '1234'})
      })
      .catch ((ret)=> {
        this.$showMessage ('上传数据失败')
      })

    },
    // 错误数据保存回掉
    callback(flag) {
      if(!flag) {
          this.$showMessage('手机记录删除失败')
          return
      }
      HostApp.__callback__ = null
      HostApp.__this__ = null
      this.download()
    },
    // 查询所有未上传斌保存在手机上的数据
    download () {
      let sql = `select * from  t_check_plan c,t_aboutuser a,t_equipmentinspection e,t_hinvestigation h where  c.f_userid = a.f_userid and a.f_userid = e.f_userid and e.f_userid = h.f_userid`
      console.log('拿到的sql',sql)
      let result = HostApp.getplan(sql)
      if (result.code === 200) {
        this.result = result.result
        if (result.result.length >0 ) {
          this.context = `有${result.result.length}条数据未上传，请点击上传按钮，自动上传`
        } else {
          this.context = '没有未上传数据'
        }
      }

    }

  },
  ready () {
    this.download ()
  }
}
</script>
<style>

</style>
